package com.ytkj.tradeserver.controller.app;

import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.StringUtils;
import com.ytkj.tradeserver.constant.ComConstants;
import com.ytkj.tradeserver.entity.Articleinfo;
import com.ytkj.tradeserver.entity.AttachmentFileVO;
import com.ytkj.tradeserver.entity.StoreArticlesVO;
import com.ytkj.tradeserver.entity.UserInfo;
import com.ytkj.tradeserver.model.PageBean;
import com.ytkj.tradeserver.model.appCom.AppHeaderVO;
import com.ytkj.tradeserver.model.appCom.AppResponseMessage;
import com.ytkj.tradeserver.service.iservice.IArticleinfoService;
import com.ytkj.tradeserver.service.iservice.IAttachmentFileService;
import com.ytkj.tradeserver.service.iservice.IUserInfoService;
import com.ytkj.tradeserver.utils.Base64Utils;
import com.ytkj.tradeserver.utils.MD5Helper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/app/news")
public class AppController {


	@Resource
	private IUserInfoService userInfoService;

	@Resource
	private IArticleinfoService articleinfoService;

	@Resource
	private IAttachmentFileService attachmentFileService;

	@ResponseBody
	@RequestMapping(value="/login", produces = "application/json; charset=utf-8",method = RequestMethod.POST)
	public String login(@RequestParam(value="reqmsg",defaultValue="") String reqmsg) {
		String jsonStr = Base64Utils.decode(reqmsg);
		UserInfo userInfo = JSON.parseObject(jsonStr, UserInfo.class);
		String changePwd = MD5Helper.createMD5(userInfo.getUpass() + ComConstants.PWD_SUFFIX);
		userInfo.setUpass(changePwd);
		userInfo = userInfoService.getUserInfo(userInfo);
		AppResponseMessage responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.SUCCESS.getValue()
				,ComConstants.RESPONSECODE.SUCCESSMSG.getValue());
		if(userInfo==null){
			responseMessage.setCode(ComConstants.RESPONSECODE.NOTFOUND.getValue());
			responseMessage.setMsg(ComConstants.RESPONSECODE.NOTFOUNDMSG.getValue());
		}else{
			userInfoService.updateUserLoginTime(userInfo);
		}
		responseMessage.setObjOne(userInfo);
		return JSON.toJSONString(responseMessage);
	}

	@ResponseBody
	@RequestMapping(value="/registe", produces = "application/json; charset=utf-8",method = RequestMethod.POST)
	public String register(@RequestParam(value="reqmsg",defaultValue="") String reqmsg) {
		String jsonStr = Base64Utils.decode(reqmsg);
		UserInfo userInfo = JSON.parseObject(jsonStr, UserInfo.class);
		AppResponseMessage responseMessage = new AppResponseMessage();
		if(StringUtils.isNullOrEmpty(userInfo.getUname())
		    ||StringUtils.isNullOrEmpty(userInfo.getUpass())
		    ||StringUtils.isNullOrEmpty(userInfo.getApptype()) ){
			responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.NOTPARAMETER.getValue()
					,ComConstants.RESPONSECODE.NOTPARAMETERMSG.getValue());
			return JSON.toJSONString(responseMessage);
		}
		String changePwd = MD5Helper.createMD5(userInfo.getUpass() + ComConstants.PWD_SUFFIX);
		int num = userInfoService.checkRepUser(userInfo);
        if(num == 0){//未被注册
			userInfo.setUpass(changePwd);
			num = userInfoService.insertUser(userInfo);
			if(num == 1){
				responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.REGSUCCESS.getValue()
						,ComConstants.RESPONSECODE.REGSUCCESSMSG.getValue());
			}else{
				responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.REGFAIL.getValue()
						,ComConstants.RESPONSECODE.REGFAILMSG.getValue());
			}
		}else{
			responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.REPEATREG.getValue()
					,ComConstants.RESPONSECODE.REPEATREGMSG.getValue());
		}

		return JSON.toJSONString(responseMessage);
	}

	//获取新闻信息
	@ResponseBody
	@RequestMapping(value="/queryNews", produces = "application/json; charset=utf-8",method = RequestMethod.POST)
	public String queryAllArticles(@RequestParam(value="reqmsg",defaultValue="") String reqmsg) {
		AppResponseMessage responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.SUCCESS.getValue()
				,ComConstants.RESPONSECODE.SUCCESSMSG.getValue());
		String jsonStr = Base64Utils.decode(reqmsg);
		AppHeaderVO reqVo = JSON.parseObject(jsonStr, AppHeaderVO.class);
        if(  StringUtils.isNullOrEmpty(reqVo.getAppType()) ){
			responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.NOTPARAMETER.getValue()
					,ComConstants.RESPONSECODE.NOTPARAMETERMSG.getValue());
			return JSON.toJSONString(responseMessage);
		}
		Articleinfo articleinfo = new Articleinfo();
		articleinfo.setAppType(reqVo.getAppType());
		articleinfo.setType(reqVo.getNewType());
		articleinfo.setTitle(reqVo.getTitle());
		if(reqVo.getPageNum()==0){
			reqVo.setPageNum(1);
		}
		if(reqVo.getPageSize()==0){
			reqVo.setPageSize(Integer.parseInt(ComConstants.PAGESIZE));
		}

		PageBean<Articleinfo> articles = articleinfoService.queryAllArticles("",articleinfo,new PageBean(reqVo.getPageNum(),reqVo.getPageSize()));
		List<Articleinfo> articleList = articles.getList();
		for(Articleinfo article : articleList){
			AttachmentFileVO file = new AttachmentFileVO(ComConstants.FILETYPE.ArticleSmallPic.getValue(),String.valueOf(article.getId()));
			article.setPictures(attachmentFileService.queryFiles(file));
		}
		articles.setPageStr("N");
		articles.setList(null);
		responseMessage.setObjOne(reqVo);
		responseMessage.setObjTwo(articles);
		responseMessage.setListOne(articleList);
		return JSON.toJSONString(responseMessage);
	}

	@ResponseBody
	@RequestMapping(value="/queryNewDetail", produces = "application/json; charset=utf-8",method = RequestMethod.POST)
	public String querySingleArticle(@RequestParam(value="reqmsg",defaultValue="") String reqmsg) {
		AppResponseMessage responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.SUCCESS.getValue()
				, ComConstants.RESPONSECODE.SUCCESSMSG.getValue());
		String jsonStr = Base64Utils.decode(reqmsg);
		AppHeaderVO reqVo = JSON.parseObject(jsonStr, AppHeaderVO.class);
        if(reqVo.getId()==0 || reqVo.getUserid()==0){
			responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.NOTPARAMETER.getValue()
					,ComConstants.RESPONSECODE.NOTPARAMETERMSG.getValue());
			return JSON.toJSONString(responseMessage);
		}
		Articleinfo articleinfo = articleinfoService.querySingleArticle(new Articleinfo(reqVo.getId()));
        if (articleinfo==null){
			responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.NOTFOUND.getValue()
					,ComConstants.RESPONSECODE.NOTFOUNDMSG.getValue());
			return JSON.toJSONString(responseMessage);
		}
		articleinfoService.addViewsNum(reqVo.getId());
		articleinfo.setLovleFlag(articleinfoService.queryIsStored(new StoreArticlesVO( reqVo.getUserid(),  reqVo.getId(),  2)));
		articleinfo.setStoreFlag(articleinfoService.queryIsStored(new StoreArticlesVO( reqVo.getUserid(),  reqVo.getId(),  1)));
		responseMessage.setObjOne(articleinfo);
		return JSON.toJSONString(responseMessage);
	}

	@ResponseBody
	@RequestMapping(value="/storeLoveNew", produces = "application/json; charset=utf-8",method = RequestMethod.POST)
	public String addStoreArticle(@RequestParam(value="reqmsg",defaultValue="") String reqmsg) {
		AppResponseMessage responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.SUCCESS.getValue()
				, ComConstants.RESPONSECODE.SUCCESSMSG.getValue());
		String jsonStr = Base64Utils.decode(reqmsg);
		AppHeaderVO reqVo = JSON.parseObject(jsonStr, AppHeaderVO.class);
		if (reqVo.getId() == 0 || reqVo.getUserid() == 0 || reqVo.getStoretype() == 0) {
			responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.NOTPARAMETER.getValue()
					, ComConstants.RESPONSECODE.NOTPARAMETERMSG.getValue());
			return JSON.toJSONString(responseMessage);
		}
		StoreArticlesVO storevo = new StoreArticlesVO( reqVo.getUserid(),  reqVo.getId(),  reqVo.getStoretype());
		boolean flag = articleinfoService.queryIsStored(storevo);
		if(flag){
           if(reqVo.getStoretype()==1){
			   responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.STORED.getValue()
					   , ComConstants.RESPONSECODE.STOREDMSG.getValue());
			   return JSON.toJSONString(responseMessage);
		   }
		   if(reqVo.getStoretype()==2){
				responseMessage = new AppResponseMessage(ComConstants.RESPONSECODE.LOVED.getValue()
						, ComConstants.RESPONSECODE.LOVEDMSG.getValue());
				return JSON.toJSONString(responseMessage);
		   }
		}
		articleinfoService.storeLoveArticle(storevo);
		return JSON.toJSONString(responseMessage);
	}

}
